package str

import (
	"strings"
)

func WordPattern(pattern string, s string) bool {
	strPatternMap := map[string]string{}
	patPatternMap := map[string]string{}
	s_pattern := ""
	p_pattern := ""

	word_list := strings.Split(s, " ")

	if len(word_list) != len(pattern) {
		return false
	}

	for i := range word_list {
		if _, ok := strPatternMap[word_list[i]]; !ok {
			strPatternMap[word_list[i]] = string(rune(len(strPatternMap)) + 'a')
		}
		s_pattern += strPatternMap[word_list[i]]

		if _, ok := patPatternMap[string(pattern[i])]; !ok {
			patPatternMap[string(pattern[i])] = string(rune(len(patPatternMap)) + 'a')
		}
		p_pattern += patPatternMap[string(pattern[i])]
	}

	return s_pattern == p_pattern
}
